from console_conpent.select import select
from effect.util.console.SuperConsole import readKey, wLine, cls

from colorama import Fore


class Options:
    def __init__(self, title, options_value, options_state):
        self.title = title
        self.options_value = options_value

        self.options_state = options_state
        self.index = 0

    def list(self):
        cls()
        print("\n" * 2)
        wLine(val=self.title)
        print("\n" * 2)
        for index, (value, state) in enumerate(zip(self.options_value, self.options_state)):

            if index == self.index:
                print("->", end=" ")

            if state:
                print(f"{Fore.GREEN}{value}{Fore.WHITE} 状态: 开启 \n")
            else:
                print(f"{Fore.RED}{value}{Fore.WHITE} 状态: 关闭\n")

        print("【空格】进行开启关闭操作, 【回车】退出当前菜单")
        return self.movepoint(readKey())

    def movepoint(self, key=None, direction=None):
        if direction is not None:
            if direction == 'up' and self.index > 0:
                self.index -= 1
            elif direction == 'up' and self.index == 0:
                self.index = len(self.options_value) - 1
            elif direction == "down" and self.index == len(self.options_value) - 1:
                self.index = 0
            elif direction == 'down' and self.index < len(self.options_value) - 1:
                self.index += 1
            return "??"
        elif key is not None:

            if key == 'UpArrow':
                self.movepoint(direction='up')
            elif key == "DownArrow":
                self.movepoint(direction='down')
            elif key == 'Spacebar':
                self.options_state[self.index] = not self.options_state[self.index]  # 设置
            elif key == 'Enter':
                return

        self.list()

    def getState(self):
        return self.options_state
